<template>
	<view class="container">
		<view class="details">
			<view class="form-item border-bottom">
				<view class="title">联系人</view>
				<input type="text" v-model="name" placeholder="请输入姓名" />
			</view>
			<view class="form-item border-bottom">
				<view class="title">手机号码</view>
				<input type="number" v-model="mobile" placeholder="请输入手机号码" />
			</view>
			<view class="address border-bottom select" @click="togglePopup">
				<view class="title">所在地区</view>
				<view class="con" v-if="selectAreaId">
					<view class="t">{{region}}</view>
				</view>
				<input type="text" disabled="disabled" v-else placeholder="请选择 省/市/区县"  />
				<image src="/static/down.png" class="icon"></image>
			</view>
			<view class="form-item border-bottom">
				<view class="title">详细地址</view>
				<input type="text" v-model="address" placeholder="请输入详细地址"  />
			</view>
			<view class="form-item">
				<view class="title" style="width: auto;">是否设为默认地址</view>
				<switch :checked="!!isDefault" color="#EA5D2E" @change="isDefaultChange" />
			</view>
		</view>
		<view class="bottom-menu" @click="submit">提交</view>
		<view class="place"></view>
		<region-picker v-if="(selectAreaId && id) || !id" :isPopupShow.sync="isPopupShow"
		 :selectProvinceId.sync="selectProvinceId"
		 :initFlag.sync="initFlag"
		 :selectCityId.sync="selectCityId"
		 :selectAreaId.sync="selectAreaId"
		:region.sync="region">
		</region-picker>
	</view>
</template>

<script>
	import { save, getDetail} from '@/api/address.js'
	import { validatemobile } from '@/utils/util.js'
	import regionPicker from '@/components/region-picker/index.vue'
	export default {
		components: {regionPicker},
		data() {
			return {
				initFlag: true,
				isPopupShow: false,
				region: '',
				selectProvinceId: '',
				selectCityId: '',
				selectAreaId: '',
				details: {},
				id: '',
				name: '',
				mobile: '',
				address: '',
				isDefault: 0
			}
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
			}
			this.setTitle(option)
			this.getAddressDetail()
		},
		onShow() {
			if (this.id) {
				this.initFlag = true
			} else {
				this.initFlag = false
			}
		},
		methods: {
			getAddressDetail() {
				if (!this.id) {
					return
				}
				const data = {
					addressId: this.id
				}
				getDetail(data).then(respanse => {
				  this.details = respanse.result
				  const {name, mobile, address, isDefault, 
				  provinceName, provinceId, cityName, cityId, 
				  districtId, districtName} = this.details
				  this.name = name
				  this.mobile = mobile
				  this.address = address
				  this.isDefault = isDefault
				  this.region = provinceName + '/' + cityName + '/' + districtName
				  this.selectProvinceId = provinceId
				  this.selectCityId = cityId
				  this.selectAreaId = districtId
				})
			},
			isDefaultChange(e) {
				this.isDefault = Number(e.target.value)
			},
			submit() {
				if (!this.name) {
					uni.showToast({
						title: '请输入姓名！',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!validatemobile(this.mobile)) {
					return
				}
				if (!this.selectAreaId) {
					uni.showToast({
						title: '请选择省/市/区县！',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!this.address) {
					uni.showToast({
						title: '请输入详细地址！',
						icon: 'none',
						duration: 2000
					})
					return
				}
				const data = {
					id: this.id,
					name: this.name,
					mobile: this.mobile,
					provinceId: this.selectProvinceId,
					cityId: this.selectCityId,
					districtId: this.selectAreaId,
					address: this.address,
					isDefault: this.isDefault
				}
				console.log(data, 'dddd')
				save(data).then(respanse => {
				  uni.navigateBack()
				})
			},
			
			togglePopup() {
				if (this.initFlag) return
				this.isPopupShow = !this.isPopupShow
			},
			setTitle(option) {
				let title = ''
				Number(option.id) ? title = '修改收货地址' : title = '添加收货地址'
				uni.setNavigationBarTitle({
					title
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.details{
		margin: 20rpx 25rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		overflow: hidden;
		.address{
			position: relative;
			display: flex;
			.title{
				padding-left: 27rpx;
				width: 196rpx;
				box-sizing: border-box;
				line-height: 115rpx;
				font-size: 30rpx;
				color: #040000;
			}
			.con{
				flex: 1;
				display: table;
				vertical-align: middle;
				height: 115rpx;
				padding-right: 60rpx;
				.t{
					display: table-cell;
					vertical-align: middle;
					line-height: 40rpx;
					font-size: 30rpx;
					color: #040000;
				}
			}
			&::after{
				left: 27rpx;
				right: 28rpx;
			}
			input{
				position: relative;
				z-index: 3;
				flex: 1;
				display: block;
				padding: 0 28rpx 0 0;
				width: 100%;
				height: 115rpx;
				box-sizing: border-box;
				font-size: 30rpx;
				color: #040000;
				background: none;
			}
			&.select{
				.icon{
					position: absolute;
					right: 28rpx;
					top: 52rpx;
					width: 27rpx;
					height: 15rpx;
				}
			}
		}
		.form-item{
			position: relative;
			height: 115rpx;
			.title{
				position: absolute;
				left: 0;
				top: 0;
				z-index: 2;
				padding-left: 27rpx;
				width: 196rpx;
				box-sizing: border-box;
				line-height: 115rpx;
				font-size: 30rpx;
				color: #040000;
			}
			switch{
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%) scale(0.7);
				font-size: 30rpx;
			}
			input{
				position: relative;
				z-index: 3;
				flex: 1;
				display: block;
				padding: 0 28rpx 0 196rpx;
				width: 100%;
				height: 115rpx;
				box-sizing: border-box;
				font-size: 30rpx;
				color: #040000;
				background: none;
			}
			&::after{
				left: 27rpx;
				right: 28rpx;
			}
		}
	}
	.bottom-menu{
		position: fixed;
		left: 25rpx;
		right: 25rpx;
		bottom: 79rpx;
		z-index: 2;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 32rpx;
		color: #fff;
		background: linear-gradient(90deg, #EB7C3F, #EA5D2E);
		border-radius: 45rpx;
	}
	.place{
		height: 169rpx;
		visibility: hidden;
	}
</style>
